/**
 * 公告管理--js
 *
 */
var notice_manage = {
	MODULE_ID : "",
	Table_Obj : null,// 表格对象
	init : function() {
		notice_manage.loadData();
		notice_manage.MODULE_ID = GetRequest().pid;
		$("#searchWords").keypress(function (e) {
			if (e.which === 13) {
				notice_manage.searchData();
			}
		});
	},
	/**
	 * 初始化
	 */
	loadData : function() {
		var obj = notice_manage.getParam();
		$.ajax({
			url : "/crm/system/notice/list", // 请求的url地址
			dataType : "json", // 返回格式为json
			data : obj, // 参数值
			type : "post", // 请求方式
			success : function(data) {
				notice_manage.initTable(data.rows);
				$("#total_records").text(data.total);
				$("#this_page").text(data.currentPage);
				$("#total_page").text(data.totalPages);
				var url = "/crm/system/notice/list";
				setCommon(obj, Table_Obj, url);
			},
			error : function() {
			}
		})
	},
	/**
	 * 检索获取条件
	 */
	getParam : function() {
		var obj = {};
		obj.pageIndex = 1;
		obj.pageSize = $("#page_size").val();
		obj.orderBy = "createTime";
		obj.orderDesc = 1;
		obj.text = $("#searchWords").val().replace(/ /g, '@#@');
		return obj;
	},
	/**
	 * 初始化表格
	 */
	initTable : function(datas) {
		Table_Obj = $("#TableContainer").easyTable({
			data : datas, // 初始数据，动态添加可以通过setData
			hideCheckbox : false, // 否显示复选框，获取复选框选择的数据用方法getCheckedItem
			// rowClick : function(data) {// 行点击回调，参数为该行数据
			// alert(JSON.stringify(data))
			// },
			rowDoubleClick : function(data) {// 行双击回调，参数为该行数据
				// alert(JSON.stringify(data))
				notice_manage.lookDetail(data.id);
			},
			columns : [ {
				title : "标题",
				field : "noticeTitle"
			}, {
				title : "内容",
				field : "noticeContent"
			}, {
				title : "创建时间",
				field : "createTime"
			}, {
				title : "创建者",
				field : "userName"
			}, {
				title : "操作",
				render : function(data) {
					return notice_manage.loadOperater(data);
				}
			} ]
		});
	},
	/**
	 * 加载方法
	 * 
	 * @param data
	 * @returns {String}
	 */
	loadOperater : function(data) {
		var html = "";
		html += '<a onclick="notice_manage.lookDetail(' + data.id + ')">查看</a>';
		html += '<a class="ml10" onclick="notice_manage.edit(' + data.id + ')">编辑</a>';
		html += '<a class="ml10" onclick="notice_manage.deleteThis(' + data.id + ')">删除</a>';
		return html;
	},
	/**
	 * 检索信息
	 */
	searchData : function() {
		var pageSize = $("#page_size").val();
		var pageIndex = 1;
		var obj = {};
		obj.pageSize = pageSize;
		obj.pageIndex = pageIndex;
		var othr = notice_manage.getParam();
		reset_serach(othr, obj);
	},
	/**
	 * 创建
	 */
	addOpen : function(){
		var addLayer = layer.open({
			type: 1
			,title: "创建公告"
			,btn: "创建"
			,area: ['500px','300px']
			,content: '<table style="width: 100%; height: 170px;">'+
				'<tr style="margin-top: 30px;">'+
				'<td><label for="noticeTitle">标题：</label></td>'+
				'<td><input type="text" class="simple_text" id="noticeTitle" maxlength="100"/>长度100字符以内</td>'+
				'</tr>'+
				'<tr style="margin-top: 30px;">'+
				'<td><label for="noticeContent">内容：</label></td>'+
				'<td><textarea class="simple_text" id="noticeContent" maxlength="1000" style="height: 70px;"></textarea>长度1000字符以内</td>'+
				'</tr>'+
				'</table>'
			,yes: function(){
				notice_manage.saveAdd(addLayer);
			}
		});
	},
	/**
	 * 查看详情
	 * 
	 * @param id id
	 */
	lookDetail : function(id) {
		$.get("/crm/system/notice/get?id=" + id, function(res) {
			if(res.status === true){
				layer.open({
					title: res.obj.noticeTitle
					,content: res.obj.noticeContent
				})
			}else {
				layer.open({
					title: "错误"
					,content: "获取失败"
				})
			}
		});
	},
	/**
	 * 编辑公告
	 */
	edit : function(id){
		$.get("/crm/system/notice/get?id=" + id, function(res){
			if(res.status) {
				var editLayer = layer.open({
					type: 1
					,title: "编辑公告"
					,btn: "保存"
					,area: ['500px','300px']
					,content: '<table style="width: 100%; height: 170px;">'+
						'<tr style="margin-top: 30px;">'+
						'<td><label for="noticeTitle">标题：</label></td>'+
						'<td><input type="text" class="simple_text" id="noticeTitle" value="' + res.obj.noticeTitle + '"  maxlength="100"/>长度100字符以内</td>'+
						'</tr>'+
						'<tr style="margin-top: 30px;">'+
						'<td><label for="noticeContent">内容：</label></td>'+
						'<td><textarea class="simple_text" id="noticeContent" maxlength="1000" style="height: 70px;">' + res.obj.noticeContent + '</textarea>长度1000字符以内</td>'+
						'</tr>'+
						'</table>'
					,yes: function(){
						notice_manage.saveEdit(res.obj, editLayer); // 传过去的是旧对象
					}
				});
			}
		})
	},
	/**
	 * 删除操作
	 */
	deleteThis : function(id) {
		layer.confirm('确定要删除吗?',{btn: ['确定', '取消'],title:"提示"}, function(){
	        $.ajax({
	            type: "post",
	            url: "/crm/system/notice/delete",
	            data: {
	            	ids:id
	            },
	            dataType: "json",
	            async:false,
	            success:function(data) {
	                if(data.status){
	                	var index=layer.alert(data.info+"请稍后刷新查看！", {icon: 1},function(){ 
	                		notice_manage.searchData();
	                		layer.close(index);
	    				});
	                   
	                }else{
	                    layer.msg(data.info, {icon: 2});
	                }
	            }
	        });
	    });
	},
	beacthDelete : function() {
		var datas = Table_Obj.getCheckedItem();
		if (datas && datas.length === 0) {
			layer.msg("未选择数据！");
			return;
		}
		var arr = [];
		$.each(datas, function(key, val) {
			arr[key] = val.id;
		});
		var ids = arr.join(",");
		notice_manage.deleteThis(ids)
	},
	saveAdd : function(addLayer){
		var noticeTitle = $("#noticeTitle").val();
		if(noticeTitle.length <= 0) {
			layer.msg("请输入标题");
			return false;
		}
		if(noticeTitle.length > 100) {
			layer.msg("标题长度在100字符以内");
			return false;
		}
		var noticeContent = $("#noticeContent").val();
		if(noticeContent.length <= 0) {
			layer.msg("请输入内容");
			return false;
		}
		if(noticeContent.length > 1000) {
			layer.msg("内容长度在1000字符以内");
			return false;
		}
		var obj ={};
		obj.noticeTitle = noticeTitle;
		obj.noticeContent = noticeContent;

		$.ajax({
			url : "/crm/system/notice/save",
			dataType : "json",
			async : true,
			data : obj,
			type : "post",
			success : function(data) {
				if(data.status){
					var index=layer.alert(data.info, {icon: 1,closeBtn: 0 },function(){
//						parent.closeCurPage();
						layer.close(index);
						layer.close(addLayer);
						notice_manage.init();
					});
				}else{
					layer.msg(data.info, {icon: 2,closeBtn: 0 });
				}
			},
			error : function() {
				layer.msg("系统错误！", {icon: 2,closeBtn: 0 });
			}
		});
	},
	saveEdit : function(notice, editLayer){
		var noticeTitle = $("#noticeTitle").val();
		if(noticeTitle.length <= 0) {
			layer.msg("请输入标题");
			return false;
		}
		if(noticeTitle.length > 100) {
			layer.msg("标题长度在100字符以内");
			return false;
		}
		var noticeContent = $("#noticeContent").val();
		if(noticeContent.length <= 0) {
			layer.msg("请输入内容");
			return false;
		}
		if(noticeContent.length > 1000) {
			layer.msg("内容长度在1000字符以内");
			return false;
		}
		if(noticeTitle === notice.noticeTitle && noticeContent === notice.noticeContent) { //没有修改直接保存，不通过网络，直接显示保存成功
			var index = layer.alert("保存成功", {icon: 1,closeBtn: 0 },function(){
//						parent.closeCurPage();
				layer.close(index);
				layer.close(editLayer);
				notice_manage.init();
			});
			// console.log("没有经过网络保存，因为未修改内容");
			return false;
		}
		var obj ={};
		obj.id = notice.id;
		obj.noticeTitle = noticeTitle;
		obj.noticeContent = noticeContent;
		obj.createTime = notice.createTime;
		obj.userUuid = notice.userUuid;
		obj.userName = notice.userName;

		$.ajax({
			url : "/crm/system/notice/save",
			dataType : "json",
			async : true,
			data : obj,
			type : "post",
			success : function(data) {
				if(data.status){
					var index = layer.alert(data.info, {icon: 1,closeBtn: 0 },function(){
//						parent.closeCurPage();
						layer.close(index);
						layer.close(editLayer);
						notice_manage.init();
					});
				}else{
					layer.msg(data.info, {icon: 2,closeBtn: 0 });
				}
			},
			error : function() {
				layer.msg("系统错误！", {icon: 2,closeBtn: 0 });
			}
		});
	}
};
